// i.r(t), i.d(t, { default: () => CursorDataModule })
import Module from "./Module"
import * as THREE from "three"
import CursorData from "../data/CursorData"
import { PointerMoveEvent } from "../event/PointerEvent"
import Logger from "../utils/Logger"
import RaycasterData from "../data/RaycasterData"
import SymbolList from "../SymbolList"
import funcUtil from "../utils/func.util"
const d = new Logger("cursor-data")
export default class CursorDataModule extends Module {
  cursorState: CursorData
  position: THREE.Vector2
  timeToFade: number
  onPointerMove: () => void
  raycasterData: any

  input: any
  constructor() {
    super(...arguments)
    this.name = "cursor-data"
    this.cursorState = new CursorData()
    this.position = new THREE.Vector2()
    this.timeToFade = 0
    this.onPointerMove = () => {
      if (null !== this.raycasterData.hit && this.raycasterData.pointerNdcPosition.distanceToSquared(this.position) > CursorDataModule.movementThreshold) {
        this.position.copy(this.raycasterData.pointerNdcPosition), (this.timeToFade = CursorDataModule.fadeOutDelay)
        const e = funcUtil.focalDistance(0, CursorDataModule.fadeInDuration, 1 - this.cursorState.opacity.value)
        this.cursorState.opacity.modifyAnimation(this.cursorState.opacity.value, 1, e), this.cursorState.commit()
      }
    }
  }
  async init(e, t) {
    this.input = await t.getModuleBySymbol(SymbolList.INPUT)
    this.raycasterData = await t.market.waitForData(RaycasterData)
    t.market.register(this, CursorData, this.cursorState)
    this.bindings.push(this.input.registerHandler(PointerMoveEvent, this.onPointerMove))
    CursorDataModule.fadeInDuration > CursorDataModule.fadeOutDelay && d.warn("fadeInDuration should be less than fadeOutDelay!")
  }
  onUpdate(e) {
    let t = !1
    this.timeToFade > 0 && ((this.timeToFade -= e), this.timeToFade <= 0 && (t = !0)),
      this.cursorState.opacity.tick(e),
      this.cursorState.commit(),
      t && this.cursorState.opacity.modifyAnimation(1, 0, CursorDataModule.fadeOutDuration)
  }

  dispose(e) {
    super.dispose(e)
  }
  static fadeOutDelay: number
  static fadeOutDuration: number
  static fadeInDuration: number
  static movementThreshold: number
}
CursorDataModule.fadeOutDelay = 700
CursorDataModule.fadeOutDuration = 700
CursorDataModule.fadeInDuration = 300
CursorDataModule.movementThreshold = 0.003
